package foo.ui;

import com.google.common.collect.Lists;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.ExpandVetoException;
import java.util.List;

public enum SwingJTreeDemo {

    instance;

    final private static Logger logger = LoggerFactory.getLogger(SwingJTreeDemo.class);

    private List<DefaultMutableTreeNode> getRandomDefaultMutableTreeNodes() {

        List<DefaultMutableTreeNode> randoms = Lists.newArrayList();
        for (int i = 0; i < RandomUtils.nextInt(10, 20); i++) {
            randoms.add(new DefaultMutableTreeNode(new User(RandomStringUtils.random(5))));
        }
        logger.info("return random of:{}", randoms);
        return randoms;

    }

    public static void main(String[] args) {
        instance.loadingTree();
    }

    public void loadingTree() {


        DefaultMutableTreeNode root = new DefaultMutableTreeNode(new User("root"), true);// 创建根节点

        DefaultMutableTreeNode sub1 = new DefaultMutableTreeNode(new User("sub1"), true);

        sub1.add(new DefaultMutableTreeNode(new User("sub1-1"), true));
        root.add(sub1);

        root.add(new DefaultMutableTreeNode(new User("sub2"), true));

        DefaultTreeModel treeModel = new DefaultTreeModel(root);

        final JTree tree = new JTree(treeModel);

        tree.collapseRow(0);

        JFrame f = new JFrame("JTreeDemo");
        f.add(tree);
        f.setSize(300, 600);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        tree.addTreeWillExpandListener(new TreeWillExpandListener() {
            @Override
            public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {
                logger.info("treeWillExpand on:{}", event.getPath());

                DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) event.getPath().getLastPathComponent();

                logger.info("treeWillExpand on treeNode:{}", treeNode);

                if (!treeNode.isRoot()) {
                    treeNode.removeAllChildren();
                    List<DefaultMutableTreeNode> randomNodes = getRandomDefaultMutableTreeNodes();
                    for (DefaultMutableTreeNode node : randomNodes) {
                        treeNode.add(node);
                    }

                    treeModel.nodeStructureChanged(treeNode);
                }

//                treeNode.removeAllChildren();
//                treeNode.removeFromParent();


//                FooUtils.silentSleep(10);
            }

            @Override
            public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException {
                logger.info("treeWillCollapse on:{}", event.getPath());
            }
        });


        tree.addTreeExpansionListener(new TreeExpansionListener() {
            @Override
            public void treeExpanded(TreeExpansionEvent event) {
                logger.info("treeExpanded on:{}", event.getPath());
            }

            @Override
            public void treeCollapsed(TreeExpansionEvent event) {
                logger.info("treeCollapsed on:{}", event.getPath());
            }
        });


        // 添加选择事件
        tree.addTreeSelectionListener(new TreeSelectionListener() {

            @Override
            public void valueChanged(TreeSelectionEvent e) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree
                        .getLastSelectedPathComponent();

                if (node == null)
                    return;

                Object object = node.getUserObject();
                if (node.isLeaf()) {
                    User user = (User) object;
                    System.out.println("你选择了：" + user.toString());
                }

            }
        });
    }


    static class User {
        private String name;

        public User(String n) {
            name = n;
        }

        // 重点在toString，节点的显示文本就是toString
        public String toString() {
            return name;
        }
    }
}
